package com.bjpowernode.web.config;

import com.bjpowernode.web.interceptor.AuthInterceptor;
import com.bjpowernode.web.interceptor.CommonsInterceptor;
import com.bjpowernode.web.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration // 表示该类是一个配置类
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private LoginInterceptor loginInterceptor;

    @Resource
    private AuthInterceptor authInterceptor;

    @Resource
    private CommonsInterceptor commonsInterceptor;

    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截器的拦截顺序和添加顺序相同
        interceptor0(registry); // 重置登录活跃时间的拦截器
        interceptor1(registry); // 登录拦截器
        interceptor2(registry); // 实名认证拦截器

    }

    // 重置登录活跃时间的拦截器
    private void interceptor0(InterceptorRegistry registry) {
        registry.addInterceptor(commonsInterceptor).addPathPatterns("/**");
    }

    // 登录拦截器
    private void interceptor1(InterceptorRegistry registry) {
        List<String> paths = new ArrayList<>();
        paths.add("/bid/bid"); // 投资
        paths.add("/user/auth/{idcard}/{name}"); // 实名认真
        paths.add("/account/balance"); // 实名认证
        paths.add("/pay"); // 充值
        registry.addInterceptor(loginInterceptor).addPathPatterns(paths);
    }

    // 实名认证拦截器
    private void interceptor2(InterceptorRegistry registry) {
        List<String> paths = new ArrayList<>();
        paths.add("/bid/bid"); // 投资
        registry.addInterceptor(authInterceptor).addPathPatterns(paths);
    }
}
